<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/13
 * author : weblinuxgame
 * filename: Base.php
 */

namespace App\Api\Requests\User;

use Illuminate\Http\Request;
use OpenApi\Annotations as OA;
use App\Core\Traits\Api\RequestParamsTrait;
use App\Core\Interfaces\RequestParamsInterface;

/**
 * @OA\Schema(
 *     schema="UserLoginRequest",
 *     title="UserLoginRequest",
 *     description="基本登陆请求体"
 * )
 * Class UserLogin
 * @package App\Api\Requests\Login
 */
class UserLogin implements RequestParamsInterface
{
    use RequestParamsTrait;

    /**
     * @OA\Property(
     *     title="用户名",
     *     format="string",
     *     description="用户注册时填写的手机号,昵称,邮箱,必须填写",
     *     required={"username"},
     * )
     * @var string
     */
    public $username;

    /**
     * @OA\Property(
     *     title="登陆密码",
     *     format="string",
     *     description="登陆密码,base64加密,必须填写",
     *     required={"password"},
     * )
     * @var string
     */
    public $password;

    /**
     * @OA\Property(
     *     title="是否记住密码",
     *     format="int",
     *     description="记住密码时,下自动登陆无需重复登陆,有效期1月",
     *     default=1,
     *     enum={0,1},
     * )
     * @var int
     */
    public $remember;

    /**
     * @OA\Property(
     *     title="验证码",
     *     format="string",
     *     description="6位验证码,开启验证时必须填写"
     * )
     * @var string
     */
    public $captcha;

    /**
     * @OA\Property(
     *     title="验证码id",
     *     format="string",
     *     description="开启验证时必须填写"
     * )
     * @var string
     */
    public $_captcha;

    protected $_validator;
    // 请求
    protected $request;
    // 规则
    protected $_rules = [
        'password' => 'bail|required',
        'username' => 'bail|required',
        'remember' => 'alpha_num',
        '_captcha' => 'alpha_dash|min:4|max:128',
        'captcha' => 'alpha_dash|min:4|max:6|captcha_check',
    ];

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->initialize($this->request);
        $this->initializeAfter();
    }

    /**
     * 初始后
     */
    protected function initializeAfter()
    {
        $this->getVars();
        if (!empty($this->password)) {
            $this->password = base64_decode($this->password);
            $this->_array['password'] = $this->password;
        }
    }

    /**
     * @inheritDoc
     */
    public function toArray()
    {
        return $this->getVars();
    }

    public function check(): bool
    {
        return $this->verify();
    }
}
